sudo: required
dist: trusty

os: linux

language: c

compiler:
  - gcc
  - clang

addons:
  apt:
    packages:
    - mysql-server
    - axel
    - cpanminus
    - libtest-base-perl
    - libtext-diff-perl
    - liburi-perl
    - libwww-perl
    - libtest-longstring-perl
    - liblist-moreutils-perl
    - libgd-dev

cache:
  apt: true
  directories:
  - download-cache

env:
  global:
    - JOBS=3
    - NGX_BUILD_JOBS=$JOBS
    - LUAJIT_PREFIX=/opt/luajit21
    - LUAJIT_LIB=$LUAJIT_PREFIX/lib
    - LUAJIT_INC=$LUAJIT_PREFIX/include/luajit-2.1
    - LUA_INCLUDE_DIR=$LUAJIT_INC
    - PCRE_VER=8.33
    - PCRE_PREFIX=/opt/pcre
    - PCRE_LIB=$PCRE_PREFIX/lib
    - PCRE_INC=$PCRE_PREFIX/include
    - OPENSSL_PREFIX=/opt/ssl
    - OPENSSL_LIB=$OPENSSL_PREFIX/lib
    - OPENSSL_INC=$OPENSSL_PREFIX/include
    - OPENSSL_VER=1.0.2j
    - LIBDRIZZLE_PREFIX=/opt/drizzle
    - LIBDRIZZLE_INC=$LIBDRIZZLE_PREFIX/include/libdrizzle-1.0
    - LIBDRIZZLE_LIB=$LIBDRIZZLE_PREFIX/lib
    - LD_LIBRARY_PATH=$LUAJIT_LIB:$LD_LIBRARY_PATH
    - DRIZZLE_VER=2011.07.21
    - TEST_NGINX_SLEEP=0.006
  matrix:
    - NGINX_VERSION=1.9.15
    - NGINX_VERSION=1.11.2

services:
 - memcache
 - redis-server

install:
  - if [ ! -d download-cache ]; then mkdir download-cache; fi
  - rm -f download-cache/drizzle7-$DRIZZLE_VER.tar.gz  # alas, we may end up with corrupted drizzle tarball stuck in the download-cache/ forever.
  - if [ ! -f download-cache/drizzle7-$DRIZZLE_VER.tar.gz ]; then wget -O download-cache/drizzle7-$DRIZZLE_VER.tar.gz http://openresty.org/download/drizzle7-$DRIZZLE_VER.tar.gz; fi
  - if [ ! -f download-cache/pcre-$PCRE_VER.tar.gz ]; then wget -O download-cache/pcre-$PCRE_VER.tar.gz http://ftp.cs.stanford.edu/pub/exim/pcre/pcre-$PCRE_VER.tar.gz; fi
  - if [ ! -f download-cache/openssl-$OPENSSL_VER.tar.gz ]; then wget -O download-cache/openssl-$OPENSSL_VER.tar.gz https://www.openssl.org/source/openssl-$OPENSSL_VER.tar.gz; fi
  - git clone https://github.com/openresty/test-nginx.git
  - git clone https://github.com/openresty/openresty.git ../openresty
  - git clone https://github.com/openresty/no-pool-nginx.git ../no-pool-nginx
  - git clone https://github.com/openresty/nginx-devel-utils.git
  - git clone https://github.com/openresty/mockeagain.git
  - git clone https://github.com/openresty/lua-cjson.git
  - git clone https://github.com/openresty/lua-upstream-nginx-module.git ../lua-upstream-nginx-module
  - git clone https://github.com/openresty/echo-nginx-module.git ../echo-nginx-module
  - git clone https://github.com/openresty/nginx-eval-module.git ../nginx-eval-module
  - git clone https://github.com/simpl/ngx_devel_kit.git ../ndk-nginx-module
  - git clone https://github.com/FRiCKLE/ngx_coolkit.git ../coolkit-nginx-module
  - git clone https://github.com/openresty/headers-more-nginx-module.git ../headers-more-nginx-module
  - git clone https://github.com/openresty/drizzle-nginx-module.git ../drizzle-nginx-module
  - git clone https://github.com/openresty/set-misc-nginx-module.git ../set-misc-nginx-module
  - git clone https://github.com/openresty/memc-nginx-module.git ../memc-nginx-module
  - git clone https://github.com/openresty/rds-json-nginx-module.git ../rds-json-nginx-module
  - git clone https://github.com/openresty/srcache-nginx-module.git ../srcache-nginx-module
  - git clone https://github.com/openresty/redis2-nginx-module.git ../redis2-nginx-module
  - git clone -b v2.1-agentzh https://github.com/openresty/luajit2.git

before_script:
  - mysql -uroot -e 'create database ngx_test; grant all on ngx_test.* to "ngx_test"@"%" identified by "ngx_test"; flush privileges;'

script:
  - cd luajit2/
  - make -j$JOBS CCDEBUG=-g Q= PREFIX=$LUAJIT_PREFIX CC=$CC XCFLAGS='-DLUA_USE_APICHECK -DLUA_USE_ASSERT' > build.log 2>&1 || (cat build.log && exit 1)
  - sudo make install PREFIX=$LUAJIT_PREFIX > build.log 2>&1 || (cat build.log && exit 1)
  - cd ..
  - tar xzf download-cache/drizzle7-$DRIZZLE_VER.tar.gz && cd drizzle7-$DRIZZLE_VER
  - ./configure --prefix=$LIBDRIZZLE_PREFIX --without-server > build.log 2>&1 || (cat build.log && exit 1)
  - make libdrizzle-1.0 -j$JOBS > build.log 2>&1 || (cat build.log && exit 1)
  - sudo make install-libdrizzle-1.0 > build.log 2>&1 || (cat build.log && exit 1)
  - cd ../mockeagain/ && make CC=$CC -j$JOBS && cd ..
  - cd test-nginx/ && sudo cpanm . && cd ..
  - cd lua-cjson/ && make -j$JOBS && sudo make install && cd ..
  - tar zxf download-cache/pcre-$PCRE_VER.tar.gz
  - cd pcre-$PCRE_VER/
  - ./configure --prefix=$PCRE_PREFIX --enable-jit --enable-utf --enable-unicode-properties > build.log 2>&1 || (cat build.log && exit 1)
  - make -j$JOBS > build.log 2>&1 || (cat build.log && exit 1)
  - sudo PATH=$PATH make install > build.log 2>&1 || (cat build.log && exit 1)
  - cd ..
  - tar zxf download-cache/openssl-$OPENSSL_VER.tar.gz
  - cd openssl-$OPENSSL_VER/
  - if [ ! -f ../download-cache/openssl-1.0.2h-sess_set_get_cb_yield.patch ]; then wget -O ../download-cache/openssl-1.0.2h-sess_set_get_cb_yield.patch https://raw.githubusercontent.com/openresty/openresty/master/patches/openssl-1.0.2h-sess_set_get_cb_yield.patch; fi
  - patch -p1 < ../download-cache/openssl-1.0.2h-sess_set_get_cb_yield.patch
  - ./config shared --prefix=$OPENSSL_PREFIX -DPURIFY > build.log 2>&1 || (cat build.log && exit 1)
  - make -j$JOBS > build.log 2>&1 || (cat build.log && exit 1)
  - sudo make PATH=$PATH install_sw > build.log 2>&1 || (cat build.log && exit 1)
  - cd ..
  - export PATH=$PWD/work/nginx/sbin:$PWD/nginx-devel-utils:$PATH
  - export NGX_BUILD_CC=$CC
  - sh util/build.sh $NGINX_VERSION > build.log 2>&1 || (cat build.log && exit 1)
  - nginx -V
  - ldd `which nginx`|grep -E 'luajit|ssl|pcre'
  - export LD_PRELOAD=$PWD/mockeagain/mockeagain.so
  - export LD_LIBRARY_PATH=$PWD/mockeagain:$LD_LIBRARY_PATH
  - export TEST_NGINX_RESOLVER=8.8.4.4
  - dig +short @$TEST_NGINX_RESOLVER openresty.org || exit 0
  - dig +short @$TEST_NGINX_RESOLVER agentzh.org || exit 0
  - prove -r t
